/*
 * Created on 18.01.2006
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package ro.qi.framework.sys.hash;

import java.util.Calendar;


/**
 * @author George Pogorelschi
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class Challenge {

	public static ChallengeEntity requestChallenge() {
		return requestChallenge(15);
	}
	
	public static ChallengeEntity requestChallenge(int challengeLifetime) {
		ChallengeEntity challengeRequest = new ChallengeEntity();
		challengeRequest.setText(generateId(16));
		Calendar today = Calendar.getInstance();
		challengeRequest.setDate(today.getTime());
		today.add(Calendar.DAY_OF_MONTH, challengeLifetime);
		challengeRequest.setExpireDate(today.getTime());
		challengeRequest.add();
		return challengeRequest;
	}

	public static String generateId() {
		return generateId(16);
	}

	public static String generateId(int size) {
		String id = "";

		while (id.length() < size)
			id += String.valueOf(Math.random()).substring(2);
		
		if (id.length() > size)
			id = id.substring(0,size);

		return id;
	}

}
